{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "# Input filename\n",
    "input_filename = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"python_tweets.json\")\n",
    "# Output filename\n",
    "labels_filename = os.path.join(os.path.expanduser(\"~\"), \"data\", \"datasets\", \"twitter\", \"python_classes.json\")\n",
    "\n",
    "tweets = []\n",
    "with open(input_filename) as inf:\n",
    "    for line in inf:\n",
    "        if len(line.strip()) == 0:\n",
    "            continue\n",
    "        tweets.append(json.loads(line))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "labels = []\n",
    "if os.path.exists(labels_filename):\n",
    "    with open(labels_filename) as inf:\n",
    "        labels = json.load(inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_next_tweet():\n",
    "    return tweets[len(labels)]['text']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div name=\"tweetbox\">\n",
       "    Instructions: Click in text box. Enter a 1 if the tweet is relevant, enter 0 otherwise.<br>\n",
       "    Tweet: <div id=\"tweet_text\" value=\"text\"></div><br>\n",
       "    <input type=text id=\"capture\"></input><br>\n",
       "</div>\n",
       "\n",
       "<script>\n",
       "function set_label(label){\n",
       "    var kernel = IPython.notebook.kernel;\n",
       "    kernel.execute(\"labels.append(\" + label + \")\");\n",
       "    load_next_tweet();\n",
       "}\n",
       "\n",
       "function load_next_tweet(){\n",
       "   var code_input = \"get_next_tweet()\";\n",
       "   var kernel = IPython.notebook.kernel;\n",
       "   var callbacks = { 'iopub' : {'output' : handle_output}};\n",
       "   kernel.execute(code_input, callbacks, {silent:false});\n",
       "}\n",
       "\n",
       "function handle_output(out){\n",
       "   console.log(out);\n",
       "   var res = out.content.data[\"text/plain\"];\n",
       "   $(\"div#tweet_text\").html(res);\n",
       "}\n",
       "\n",
       "$(\"input#capture\").keypress(function(e) {\n",
       "    console.log(e);\n",
       "      if(e.which == 48) {\n",
       "        // 0 pressed\n",
       "        set_label(0);\n",
       "        $(\"input#capture\").val(\"\");\n",
       "      }else if (e.which == 49){\n",
       "        // 1 pressed\n",
       "        set_label(1);  \n",
       "        $(\"input#capture\").val(\"\");\n",
       "      }\n",
       "});\n",
       "\n",
       "load_next_tweet();\n",
       "</script>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<div name=\"tweetbox\">\n",
    "    Instructions: Click in text box. Enter a 1 if the tweet is relevant, enter 0 otherwise.<br>\n",
    "    Tweet: <div id=\"tweet_text\" value=\"text\"></div><br>\n",
    "    <input type=text id=\"capture\"></input><br>\n",
    "</div>\n",
    "\n",
    "<script>\n",
    "function set_label(label){\n",
    "    var kernel = IPython.notebook.kernel;\n",
    "    kernel.execute(\"labels.append(\" + label + \")\");\n",
    "    load_next_tweet();\n",
    "}\n",
    "\n",
    "function load_next_tweet(){\n",
    "   var code_input = \"get_next_tweet()\";\n",
    "   var kernel = IPython.notebook.kernel;\n",
    "   var callbacks = { 'iopub' : {'output' : handle_output}};\n",
    "   kernel.execute(code_input, callbacks, {silent:false});\n",
    "}\n",
    "\n",
    "function handle_output(out){\n",
    "   console.log(out);\n",
    "   var res = out.content.data[\"text/plain\"];\n",
    "   $(\"div#tweet_text\").html(res);\n",
    "}\n",
    "\n",
    "$(\"input#capture\").keypress(function(e) {\n",
    "    console.log(e);\n",
    "      if(e.which == 48) {\n",
    "        // 0 pressed\n",
    "        set_label(0);\n",
    "        $(\"input#capture\").val(\"\");\n",
    "      }else if (e.which == 49){\n",
    "        // 1 pressed\n",
    "        set_label(1);  \n",
    "        $(\"input#capture\").val(\"\");\n",
    "      }\n",
    "});\n",
    "\n",
    "load_next_tweet();\n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(labels_filename, 'w') as outf:\n",
    "    json.dump(labels, outf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
